﻿using System;
using System.Collections;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Xml;
using System.Xml.Linq;

#region Usings
using System.Collections.Generic;
using System.Linq;
using System.IO.Packaging;

#endregion
namespace INTEX.DataBase.Tools
{
 　
    public class DataToXml
    {  

        public static string CompressLongString(string longString)
        {
            return longString;
            MemoryStream outstream = new MemoryStream();
            MemoryStream instream = new MemoryStream(Encoding.UTF8.GetBytes(longString));

            using (GZipStream compress =
                new GZipStream(outstream,
                CompressionMode.Compress))
            {
                instream.CopyTo(compress); 
            } 
            return Encoding.Unicode.GetString(outstream.GetBuffer());
        }
        private static void PutCloud(string xml, string table)
        {
            try
            {
            }
            catch (Exception ex)
            {
            }
        }
        private static XDocument DocumentToXDocumentReader(XmlDocument doc)
        {
            return XDocument.Load(new XmlNodeReader(doc));
        }
        public static XDocument Convert2XML(string root, DataTable tb, string key, ArrayList keylist = null, bool sendback = true, bool SaveXML = false)
        {
            string table = tb.TableName;
            string path =root + "\\" + tb.TableName;
            string result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root></root>";
            XmlDocument doc = null;
            XDocument xdoc=null;
            try
            {
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                tb.DataSet.DataSetName = table;
     
                if (sendback)
                {

                    if (SaveXML)
                    {
                        path = root + "\\" + tb.TableName + "\\" + table + System.DateTime.Now.ToFileTime() + ".xml.ITX";
                    }
                    else
                    {
                        path = root + "\\" + tb.TableName + "\\" + table + System.DateTime.Now.ToFileTime() + ".XML";
                    }
                    tb.WriteXml(path, XmlWriteMode.IgnoreSchema);
                    doc = new XmlDocument();
                    doc.Load(path);
                    xdoc = DocumentToXDocumentReader(doc);
                    if (!SaveXML)
                    {
                        System.IO.File.Delete(path);
                    } 
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {

            }
            return xdoc;
        }
        public const string start = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        /**/
        /// <summary>
        /// 将DataTable对象转换成XML字符串
        /// </summary>
        /// <param name="dt">DataTable对象</param>
        /// <returns>XML字符串</returns>
        public static string CDataToXml(DataTable dt)
        {
            if (dt != null)
            {
                MemoryStream ms = null;
                XmlTextWriter XmlWt = null;
                try
                {
                    ms = new MemoryStream();
                    //根据ms实例化XmlWt
                    XmlWt = new XmlTextWriter(ms, Encoding.Unicode);
                    //获取ds中的数据
                    dt.WriteXml(XmlWt);
                    int count = (int)ms.Length;
                    byte[] temp = new byte[count];
                    ms.Seek(0, SeekOrigin.Begin);
                    ms.Read(temp, 0, count);
                    //返回Unicode编码的文本
                    UnicodeEncoding ucode = new UnicodeEncoding();

                    string returnValue = start + ucode.GetString(temp).Trim();
                    return returnValue;
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    //释放资源
                    if (XmlWt != null)
                    {
                        XmlWt.Close();
                        ms.Close();
                        ms.Dispose();
                    }
                }
            }
            else
            {
                return "";
            }
        }
        /**/
        /// <summary>
        /// 将DataSet对象中指定的Table转换成XML字符串
        /// </summary>
        /// <param name="ds">DataSet对象</param>
        /// <param name="tableIndex">DataSet对象中的Table索引</param>
        /// <returns>XML字符串</returns>
        public static string CDataToXml(DataSet ds, int tableIndex)
        {
            if (tableIndex != -1)
            {
                return CDataToXml(ds.Tables[tableIndex]);
            }
            else
            {
                return CDataToXml(ds.Tables[0]);
            }
        }
        /**/
        /// <summary>
        /// 将DataSet对象转换成XML字符串
        /// </summary>
        /// <param name="ds">DataSet对象</param>
        /// <returns>XML字符串</returns>
        public static string CDataToXml(DataSet ds)
        {
            return CDataToXml(ds, -1);
        }
        /**/
        /// <summary>
        /// 将DataView对象转换成XML字符串
        /// </summary>
        /// <param name="dv">DataView对象</param>
        /// <returns>XML字符串</returns>
        public static string CDataToXml(DataView dv)
        {
            return CDataToXml(dv.Table);
        }
    }


    /**/
    /// <summary>
    /// XML形式的字符串、XML文江转换成DataSet、DataTable格式
    /// </summary>
    public class XmlToData
    {
        /**/
        /// <summary>
        /// 将Xml内容字符串转换成DataSet对象
        /// </summary>
        /// <param name="xmlStr">Xml内容字符串</param>
        /// <returns>DataSet对象</returns>
        public static DataSet CXmlToDataSet(string xmlStr)
        {
            if (!string.IsNullOrEmpty(xmlStr))
            {
                StringReader StrStream = null;
                XmlTextReader Xmlrdr = null;
                try
                {
                    DataSet ds = new DataSet();
                    //读取字符串中的信息
                    StrStream = new StringReader(xmlStr);
                    //获取StrStream中的数据
                    Xmlrdr = new XmlTextReader(StrStream);
                    //ds获取Xmlrdr中的数据                
                    ds.ReadXml(Xmlrdr);
                    return ds;
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    //释放资源
                    if (Xmlrdr != null)
                    {
                        Xmlrdr.Close();
                        StrStream.Close();
                        StrStream.Dispose();
                    }
                }
            }
            else
            {
                return null;
            }
        }
        /**/
        /// <summary>
        /// 将Xml字符串转换成DataTable对象
        /// </summary>
        /// <param name="xmlStr">Xml字符串</param>
        /// <param name="tableIndex">Table表索引</param>
        /// <returns>DataTable对象</returns>
        public static DataTable CXmlToDatatTable(string xmlStr, int tableIndex)
        {
            return CXmlToDataSet(xmlStr).Tables[tableIndex];
        }
        /**/
        /// <summary>
        /// 将Xml字符串转换成DataTable对象
        /// </summary>
        /// <param name="xmlStr">Xml字符串</param>
        /// <returns>DataTable对象</returns>
        public static DataTable CXmlToDatatTable(string xmlStr)
        {
            return CXmlToDataSet(xmlStr).Tables[0];
        }
    }
}